package ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * A class to get project info from the user.
 * @author darin
 *
 */
public class ProjectEntryDialog extends JDialog implements ActionListener {
	/**
	 *  controls the width of the window.
	 */
	private static final int DEFAULT_WIDTH = 200;
	/**
	 * Controls the height of the window.
	 */
	private static final int DEFAULT_HEIGHT = 100;
	/**
	 * where the name of the project is stored.
	 */
	private JTextField name;
	/**
	 * where the description of the project is stored.
	 */
	private JTextField description;
	/**
	 * accepts the entry.
	 */
	private JButton okButton;
	/**
	 * rejects the entry.
	 */
	private JButton cancelButton;
	/**
	 * true if the ok button was clicked
	 */
	private boolean ok;

	/**
	 * A dialog box for project entry.
	 */
	public ProjectEntryDialog()	{
		JLabel nameLabel = new JLabel("Name");
		JLabel descriptionLabel = new JLabel("Description");
		setTitle("Project Entry");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLayout(new BorderLayout());

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2, 2));
		panel.add(nameLabel);
		name = new JTextField();
		panel.add(name);
		panel.add(descriptionLabel);
		description =  new JTextField();
		panel.add(description);
		add(panel, BorderLayout.NORTH);

		okButton = new JButton("Ok");
		okButton.addActionListener(this);
		okButton.setActionCommand("Ok");
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		cancelButton.setActionCommand("Cancel");
		JPanel southPanel = new JPanel();
		southPanel.add(okButton);
		southPanel.add(cancelButton);
		add(southPanel, BorderLayout.SOUTH);
	}

	/**
	 * @return String the contents of the name field.
	 */
	public final String getProjectName()	{
		return name.getText();
	}
	/**
	 * @return String, the contents of the description field.
	 */
	public final String getProjectDescription() {
		return description.getText();
	}
	/**
	 *
	 * @return boolean true if ok button was clicked.
	 */
	public final boolean getFlag() {
		// this is dangerous.  If some previous use set ok to true, then 
		// user opens and starts entering something, then closes the dialog
		//without hitting cancel or ok, Ok will still be set
		// I'd like a way to set ok to false each time setVisible is called.
		return ok;
	}

	@Override
	public final void actionPerformed(final ActionEvent e) {
		System.out.println(e.getActionCommand());
		if (e.getActionCommand().equals("Ok")) {
			ok = true;
		} else {
			ok = false;
		}
		setVisible(false);
	}
}
